﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Net.Mail;
using System.IO;
using System.ComponentModel;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Xml.Serialization;
using System.Web.Configuration;

namespace MensUnoShop.Models
{
    public static class Config
    {
        public static readonly string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        public static readonly string key = System.Configuration.ConfigurationManager.AppSettings["key"];
        public static readonly string logpath = System.Configuration.ConfigurationManager.AppSettings["logpath"];
        public static readonly string tradelog = Path.Combine(Config.logpath, "Trade_"+System.DateTime.Today.ToString("yyyy_MM_dd")+".log");
        public static readonly string uploadpath = System.Configuration.ConfigurationManager.AppSettings["uploadpath"];
        public static readonly string Latter_Join = System.Configuration.ConfigurationManager.AppSettings["Latter_Join"];
        public static readonly string Latter_Join_Success = System.Configuration.ConfigurationManager.AppSettings["Latter_Join_Success"];
        public static readonly string Latter_Forget_Password = System.Configuration.ConfigurationManager.AppSettings["Latter_Forget_Password"];
        public static readonly string Latter_Mail_To_Friend = System.Configuration.ConfigurationManager.AppSettings["Latter_Mail_To_Friend"];
        public static readonly string Latter_Mail_Order = System.Configuration.ConfigurationManager.AppSettings["Latter_Order"];
        public static readonly string Latter_Mail_Order_Shipping = System.Configuration.ConfigurationManager.AppSettings["Latter_Order_Shipping"];
        public static readonly string Latter_Mail_Order_Return = System.Configuration.ConfigurationManager.AppSettings["Latter_Order_Return"];
        public static readonly string Latter_Mail_Order_Refund = System.Configuration.ConfigurationManager.AppSettings["Latter_Order_Refund"];
        public static readonly string SMTPServer = System.Configuration.ConfigurationManager.AppSettings["SMTPServer"];
        public static readonly string[] systemAccount = System.Configuration.ConfigurationManager.AppSettings["sysaccount"].Split(',');
        public static readonly string[] hotkeycolor = System.Configuration.ConfigurationManager.AppSettings["HotKeyColor"].Split(',');
        public static readonly int hotkeycount = int.Parse(System.Configuration.ConfigurationManager.AppSettings["HotKeyCount"]);
        

        public static readonly string MerchantNumber1 = System.Configuration.ConfigurationManager.AppSettings["MerchantNumber1"];
        public static readonly string R_Code1 = System.Configuration.ConfigurationManager.AppSettings["R_Code1"];
        public static readonly string Code1 = System.Configuration.ConfigurationManager.AppSettings["Code1"];
        public static readonly string PayLink1 = System.Configuration.ConfigurationManager.AppSettings["PayLink1"];
        public static readonly string OrderURL1 = System.Configuration.ConfigurationManager.AppSettings["OrderURL1"];
        public static readonly string ReturnURL1 = System.Configuration.ConfigurationManager.AppSettings["ReturnURL1"];

        public static readonly string MerchantNumber2 = System.Configuration.ConfigurationManager.AppSettings["MerchantNumber2"];        
        public static readonly string Code2 = System.Configuration.ConfigurationManager.AppSettings["Code2"];
        public static readonly string PayLink2 = System.Configuration.ConfigurationManager.AppSettings["PayLink2"];
        public static readonly string QueryLink2 = System.Configuration.ConfigurationManager.AppSettings["QueryLink2"];

        public static readonly string Site = System.Configuration.ConfigurationManager.AppSettings["Site"];

        public static readonly string[] ShippingWay = new string[] { "台灣地區免運費 (限時掛號、包裹)", "亞洲Asia(EMS/國際快捷郵件)", "美洲或歐洲America and Europe(EMS/國際快捷郵件)", "中美洲或歐洲America and Europe(EMS/國際快捷郵件)" };
        public static readonly string[] InvoiceType = new string[] { "二聯式","三聯式" };

        public enum TagType
        {
            [Description("分類標籤")]
            CLASS = 1,        
            [Description("系列標籤")]
            GROUP,         
            [Description("色彩群組標籤")]
            COLORGOURP,     
            [Description("商品標籤")]
            PRODUCT,       
            [Description("規格標籤")]
            SPEC,         
            [Description("自由標籤")]
            FREE,         
            [Description("合購標籤")]
            COMBO          
        }

        public enum OrderStatusType
        {
            [Description("訂單成立")]
            Init = 0,       //訂單成立
            [Description("等待付款")]
            Waiting,        //等待付款1
            [Description("申請退貨")]
            ApplyReturn,    //申請退貨2
            [Description("付款完成")]
            Payment,        //付款完成(待出貨)3
            [Description("待退款")]
            Refunding,      //待退款4
            [Description("退貨完成")]
            Refunded,       //退貨完成5
            [Description("交易失敗")]
            Fail,           //交易失敗6
            [Description("訂單取消")]
            Cannel,         //訂單取消7
            [Description("已出貨")]
            Shipping,       //已出貨8
            [Description("出貨中")]
            PreShipping     //出貨中9
        }

        /// <summary>
        /// 賣場類型
        /// </summary>
        public enum MarketType
        {
            Normal = 1,  //一般
            Mangazine,   //雜誌
            Combo        //合購
        }

        /// <summary>
        /// 訂單行銷類型
        /// </summary>
        public enum SalesKitType
        {
            PriceDiscount = 1,  //折價
            Gift                //送贈品
        }

        /// <summary>
        /// 行銷機制類型
        /// </summary>
        public enum SalesKitRulesType
        {
            ByList = 1,  //撈單
            ByOrder,     //隨訂單
            ByMarket,    //隨賣場
            ByLogin      //隨登入
        }

        /// <summary>
        /// 訂單折抵(固定)類型
        /// </summary>
        public enum OrderDiscountType
        {
            SalesKitDiscount = 1,        //行銷活動折抵
            CouponDiscount,
            BirthDiscount,
            HanselDiscount,
            ComboDiscount
        }

        public enum SalesKitKind
        {
            BirthdayDiscount = 1,   // 生日折扣
            HanselDiscount,         // 買千折百
            ComboDiscount           // 合購折扣
        }

        public static bool SendMail(string title, string strMemo, string tomail, string toname)
        {
            try
            {
                MailMessage mail = new MailMessage();
                string smtpStr = Config.SMTPServer;
                mail.From = new MailAddress(Config.systemAccount[0], "Men's Uno Shop");
                mail.Subject = title;
                mail.Body = strMemo;
                mail.IsBodyHtml = true;
                mail.To.Add(new MailAddress(tomail, toname));
                SmtpClient smtpMail = new SmtpClient(smtpStr);
                smtpMail.Credentials = new System.Net.NetworkCredential(Config.systemAccount[0], Config.systemAccount[1]);
                smtpMail.Port = 25;
                smtpMail.EnableSsl = false;
                smtpMail.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                SenerPro.Logger log = new SenerPro.Logger(Config.logpath);
                log.RecordExceptionFormat(log.FlowLogPath, "SendMail", ex.ToString());
                return false;
            }
        }

        #region 字串處理--任意放置*號
        public static string RandomStar(string oristr, int stype)
        {
            //字串長度
            int slength = oristr.Length;
            //要放幾個*
            if (stype == 0) //姓名
            {
                for (int i = 0; i < oristr.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        oristr = oristr.Replace(oristr[i], '*');
                    }
                }
            }
            else if (stype == 1) //電話
            {
                oristr = oristr.Substring(0, (slength / 2)).PadRight(slength, '*');
            }
            else //其他
            {
                oristr = oristr.Substring((slength / 2)).PadLeft(slength, '*');
            }
            return oristr;
        }
        #endregion

        #region enum相關
        /// <summary>
        /// 取得enum內列舉資料的說明
        /// </summary>
        public static string GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString();
        }

        /// <summary>
        /// 取得指定的enum列表
        /// </summary>
        public static List<SelectListItem> GetEnumList(Type EnumName)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            Array keys = Enum.GetValues(EnumName);
            for (int i = 0; i < keys.Length; i++)
                list.Add(new SelectListItem() { Text = GetEnumDescription((Enum)Enum.ToObject(EnumName, keys.GetValue(i))), Value = ((int)keys.GetValue(i)).ToString() });

            return list;
        }
        #endregion

    }
}